{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用梯度上升法求解主成分 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.empty((100, 2))            #虚拟测试用例，具有2个特征\n",
    "X[:,0] = np.random.uniform(0., 100., size=100)            #特征1是0-100随机分布，100个样本\n",
    "X[:,1] = 0.75 * X[:,0] + 3. + np.random.normal(0, 10., size=100)    #特征2 和特征1 有基本线性关系，这样降维效果比较明显"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0xfc5c287f48>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD4CAYAAAAXUaZHAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAakklEQVR4nO3db4xc5XXH8e/xeiHrRMmasIlgodipXAgNCg4jROIqApOIkETBglRJGrVUQuJN2gClJOv2RRKpLY6IQlI1imRBWtqgYAKWIaEKirBRVSTc7MYQ49gUSorxQsJG8ZIKb8vanL6Yu3h29s7MnZn757n3/j6StZ7xzt7neuxznznPuecxd0dERKplVdEDEBGR9Cm4i4hUkIK7iEgFKbiLiFSQgruISAWtzvNgp59+uq9bty7PQ4qIlN7MzMyv3X2in9fkGtzXrVvH9PR0nocUESk9M3u+39coLSMiUkEK7iIiFaTgLiJSQQruIiIVpOAuIlJBuVbLiIikbde+WW57+GlenF/gzPExbrniXL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n5dZ3eb5ppmW9FsXA/zaqinsdQ+wERkQoKNS0jIiJDUHAXEakgBXcRkQpScBcRqSAFdxGRClJwFxGpIAV3EZEK+n/E/kFsoZ+C0gAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X[:,0], X[:,1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## demean-样本均值归0 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def demean(X):\n",
    "    return X - np.mean(X, axis=0)            #均值归零 和for循环等价；每一列的均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_demean = demean(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0xfc5c345a48>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXkAAAD4CAYAAAAJmJb0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAakElEQVR4nO3df4xdZZkH8O+XYcBhXR2UQWHa2CZLYFVcKzdo0j9WqggigQbciGtcdjUhZjEBF5FB/nETDTUkYow/Nl01wQQDrMXSFQyirX8s2aJ3KIWttNLgCh1YGbMMuumo0+mzf9xzp3funPvjnPO+5z3nvd9P0jD33B/nHKZ9znuf9znPSzODiIjE6aTQByAiIv4oyIuIRExBXkQkYgryIiIRU5AXEYnYyaEPoNMZZ5xhGzZsCH0YI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emNkxkp8E8DCAMQDfNrMDgQ/Lp80APgrgKZJPJNs+C2AbgPtIfhytKrO/CXR8odwC4B6SnwewD60LYWWprYGISMSUrhERiZiCvIhIxBTkRUQipiAvIhIxBXkRkYgpyIuIRExBXkQkYv8PdDaRMnaB2f8AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X_demean[:,0], X_demean[:,1])     #图像原点位于图像大致中心的位置；在两个方向上的均值都为0\n",
    "                                              #可用 np.mean(X_demean[:,0])，np.mean(X_demean[:,1])查看两个方向的均值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 梯度上升法 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(w, X):                  #目标函数，X已经归零化\n",
    "    return np.sum((X.dot(w)**2))/len(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def df_math(w, X):            #求目标函数的梯度\n",
    "    return X.T.dot(X.dot(w)) * 2./len(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "#验证上述梯度求解的正确性\n",
    "def df_debug(w, X, epsilon=0.0001):           # w 是方向向量，模为1，维度很小，所以epsioon取值很小\n",
    "    res = np.empty(len(w))\n",
    "    for i in range(len(w)):\n",
    "        w_1 = w.copy()\n",
    "        w_1[i] += epsilon\n",
    "        w_2 = w.copy()\n",
    "        w_2[i] -= epsilon\n",
    "        res[i] = (f(w_1, X) - f(w_2, X))/(2 * epsilon)\n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def direction(w):                  #求模\n",
    "    return w/np.linalg.norm(w)\n",
    "\n",
    "def gradient_ascent(df, X, initial_w, eta, n_iters = 1e4, epsilon=1e-8):    #梯度上升法的过程，与之前的梯度下降法过程一样\n",
    "    w = direction(initial_w)                       # 转换为单位向量\n",
    "    cur_iter = 0\n",
    "    while cur_iter<n_iters:\n",
    "        gradient = df(w, X)\n",
    "        last_w = w\n",
    "        w = w + eta * gradient                     # 上升+ ；下降-\n",
    "        w = direction(w)                           # 调用函数，每次都让 w 成为一个单位方向\n",
    "        if(abs(f(w, X) - f(last_w, X))<epsilon):\n",
    "            break\n",
    "        cur_iter += 1\n",
    "    return w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.13109123, 0.81245425])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "initial_w = np.random.random(X.shape[1])     #初始化的值不能用0向量；否则每次的结果都是没有方向的0向量\n",
    "initial_w                                    #随机一个初始向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "eta = 0.001          # pca不能用StandardScaler进行数据标准化；标准化后标准差为1 没有最大值了。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.75432912, 0.65649644])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gradient_ascent(df_debug, X_demean, initial_w, eta)      #先使用笨办法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.75432912, 0.65649644])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gradient_ascent(df_math, X_demean, initial_w, eta)       #得到相同结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "w = gradient_ascent(df_math, X_demean, initial_w, eta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0xfc5c300d08>]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXkAAAD4CAYAAAAJmJb0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAeXklEQVR4nO3df5RddXnv8feTYQgDFgchCExSk1YaLhAkMI3YrK6LKQoihsiPgnCBq3ShS7wXbUDCD5W6aJmWClwWUsxFK1gwhAZjbFAIJFalBJhJCBABzQVBhtAMlYFCpjCZPPePcyY5ObPP7/37fF5rZTHn596bSZ793c/32c/X3B0REcmnSUnvgIiIREdBXkQkxxTkRURyTEFeRCTHFORFRHJst6R3oNR+++3n06dPT3o3REQyZWBg4FV3nxL0WqqC/PTp0+nv7096N0REMsXMXqj0mtI1IiI5piAvIpJjCvIiIjmmIC8ikmMK8iIiOZaq6hoRkbAtXz/Itfc9y8vDIxzU3cUlx89kweyepHcrNgryIpJby9cPctk9TzIyOgbA4PAIl93zJEDbBHqla0Qkt66979kdAX7cyOgY1973bEJ7FD8FeRHJrZeHRxp6Po8U5EUktw7q7mro+TxSkBeR3Lrk+Jl0dXbs8lxXZweXHD8zoT2KnyZeRSS3xidXVV0jIpJTC2b3tFVQL6d0jYhIjinIi4jkmIK8iEiOKciLiOSYgryISI4pyIuI5JiCvIhIjqlOXkRyr53bDSvIi0iupaHdcOlJpnvPTtzh9ZHRWE44SteISK4l3W54/CQzODyCA69tHWV4ZBRn5wln+frByLavIC8iuZZ0u+Ggk0ypqE84CvIikmtJtxuu52QS5QlHQV5Eci3pdsP1nEyiPO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Vj+iaF3/ehwKeKx55X24CF7v7fgGOAC4vHuwh40N0PBh4sPs6zi4CnSx7/LXB98fhfA85PZK/qpCDfuOuBLwOlM9YnA7d7wVqg28wOTGTvIuTu97v7tuLDtcDU4s8nA0vc/W13fx7YBMxJYh8jNgfY5O7Pufs7wBIKx55L7r7Z3dcVf/5PCoGuh8Ix31Z8223AgmT2MHpmNhX4OHBr8bEB84B/Lr4l9cevIN8AM5sPDLr7hrKXeoDfljx+qfhcnn0G+HHx53Y5/nY5zgnMbDowG3gEeK+7b4bCiQDYP7k9i9wNFAZ124uP9wWGSwY7qf87oJWhypjZA8ABAS9dAVwOfDToYwHPZbI2tdrxu/sPi++5gsKl/B3jHwt4fyaPv4Z2Oc5dmNm7gGXAF939jcJgNv/M7CRgi7sPmNmx408HvDXVfwcU5Mu4+3FBz5vZLGAGsKH4l3wqsM7M5lA4m08reftU4OWIdzUSlY5/nJmdB5wE/JnvvMkiN8dfQ7sc5w5m1kkhwN/h7vcUn/53MzvQ3TcX05JbktvDSM0F5pvZicAewN4URvbdZrZbcTSf+r8DStfUyd2fdPf93X26u0+n8A/+KHd/BVgBnFussjkGeH38cjZPzOwE4FJgvrtvLXlpBXCmmU02sxkUJqAfTWIfI/YYcHCxumJ3CpPNKxLep8gU88/fBp529+tKXloBnFf8+Tzgh3HvWxzc/TJ3n1r8934msNrdzwbWAKcV35b649dIPhz3AidSmHDcCnw62d2JzE3AZGBV8Wpmrbt/zt03mtlS4JcU0jgXuvtYgvsZCXffZmZfAO4DOoDvuPvGhHcrSnOBc4Anzezx4nOXA33AUjM7n0KV2ekJ7V9SLgWWmNnVwHoKJ8LUUlsDEZEcU7pGRCTHFORFRHJMQV5EJMcU5EVEckxBXkQkxxTkRURyTEFeRCTH/j8EgYlEyHBBuwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X_demean[:,0], X_demean[:,1])                #可视化\n",
    "plt.plot([0, w[0]*30], [0, w[1]*30], color='red')        # w 是一个方向向量，非常小，乘一个系数来扩大它，以便观察"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "#第一主成分求得\n",
    "#对于二维数据，求得第一主成分就足够，而对于更多维度，可能不止一个主成分"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用极端数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "X2 = np.empty((100, 2))\n",
    "X2[:,0] = np.random.uniform(0., 100., size=100)\n",
    "X2[:,1] = 0.75 * X2[:,0] + 3."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0xfc5c4292c8>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD7CAYAAABzGc+QAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAalElEQVR4nO3df3BU93nv8ffDemmE21xBLPuCjIrtMqRxqCHV2HLpdFLIjfPLRuHGTohpmZYJ/aP3Nok9NNCrCXjGKWRo4+TOdDxD46TkmlIIJgvJzYRmiDOdemo1IsKWiU39I45goUZJUJobdGMhnv6xZ/Gy7OqcXe3ZH2c/rxlG2qMj77Ne++Ovn/3+MHdHRERaz6xGFyAiItVRgIuItCgFuIhIi1KAi4i0KAW4iEiLUoCLiLSoSAFuZp80sxNm9pyZ7TWzN5nZTWY2aGYvmtk+M5sdd7EiIvKG0AA3s27gz4Bed387kAI+An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4lSS+38Becn3+SXIj7A3l3l9yLZS/CXJuhNwsndDn0FJ6EZEW1ewtFBERKUMBLiLSohTgIiItSgEuItKiFOAiIi1KAS4i0qIU4CIiLeo/AStybNE0PDnbAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X2[:,0], X2[:,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "X2_demean = demean(X2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "w2 = gradient_ascent(df_math, X2_demean, initial_w, eta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0xfc5c44f5c8>]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXkAAAD7CAYAAACPDORaAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAe20lEQVR4nO3de5SVdb3H8feX7QYHTQcVFQYmzEOUSEpNgHpOmWjgFSK8IBoZK6qjpUtDRDhhpkdsllnm5Yim6ZHESziSqRMqZnqEhEYERBLwAgMJJoPlTDLMfM8fe+9hz7BnZs/sZ98/r7VY7Ocy+/k9i8WHH7/n93x/5u6IiEhh6pHtBoiISPoo5EVECphCXkSkgCnkRUQKmEJeRKSAKeRFRApYYCFvZiEzqzGzJ6LbR5jZMjN708weMrOeQV1LRESSE2RP/lJgbdz2jcDN7j4Y2AFMDfBaIiKSBAviZSgz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eukG9HJguz5ZL27jvGzKYAZwCjfc9LFnl/350o9PtrxczCRAJ+vrsvjO5+z8z6ufvW6BDktuy1MHAnAGeZ2WnAvsABRHr2pWa2T7Q3n7d/5hqu6SJ3X+Xuh7r7oGgph83A5939b8Ai4BvRWTajSLIwW74ws7HADOAsd6+PO7QIOM/MepnZEUQePP85G21Mk1eAwdHZFj2JPGRelOU2pUV0LPpXwFp3/1ncoUXAlOjnKcDjmW5burj7THcfEP37fB7wnLtPBpYAE6On5e09qycfrEIvzHYr0AtYHP1fzFJ3/667rzGzh4HXiQzjXOzuTVlsZ6DcfbeZXQJUAyHgHndfk+VmpcsJwIXAKjN7NbrvamAu8LCZTSUyo+zsLLUvk2YAC8zsOqCGyD9+eUdlDURECpiGa0RECphCXkSkgCnkRUQKmEJeRKSAKeRFRAqYQl5EpIAp5EVECtj/A/vSC7qt8F96AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X2_demean[:,0], X2_demean[:,1])\n",
    "plt.plot([0, w2[0]*30], [0, w2[1]*30], color='r')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
